import { EntityManager, MikroORM } from '@mikro-orm/mysql';
import { InjectEntityManager, InjectMikroORM } from '@mikro-orm/nestjs';
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Users } from "@entities/Users";
import { knexPrefix } from "@databases/vo/dao-helper";
import { UserskVO } from "@databases/vo/UserVO";
import { JsonApiResponse } from '@utils/json.api.response';
import { OrderJobEnrolls } from "@entities/OrderJobEnrolls";
import { JobTasks } from "@entities/JobTasks";
import { LoginRecoders } from "@entities/LoginRecoders";
import { knext } from 'knext';

@ApiTags('(站点) 用户')
@Controller('user')
export class UserController {
    constructor(
        @InjectMikroORM('zp')
        private readonly orm : MikroORM,
        @InjectEntityManager('zp')
        private readonly em : EntityManager
        
    ){}

    @Get('search')
    async search(@Query('keyword') keyword) {
        const knex = knext(this)
        const users = await knex
        .fromORM(Users,'users')
        .select(
            ...knexPrefix(UserskVO.baseInfo())
        )
        .where('name','LIKE',`%${keyword}%`)
        .orWhere('id_number','LIKE',`%${keyword}%`)
        .orWhere('phone_number','LIKE',`%${keyword}%`)
        .limit(10)
        .populate()
         
        return JsonApiResponse.new(users)
    }

    @ApiOperation({summary:'获取所有用户'})
    @Get('list')
    async list() {
        const knex = knext(this)
        const users = await knex
        .fromORM(Users,'users')
        .select(
            ...knexPrefix(UserskVO.baseInfo())
        )
        .populate()
         
        return JsonApiResponse.new(users)
    }

    @Get('list/mvp')
    async listMvp(){
        const knex = knext(this)

        const task_ids = await knex
        .fromORM(OrderJobEnrolls)
        .distinct()
        .pluck('job_task_id')
        .limit(10)
        .populate()

        const creator_ids = await knex
        .fromORM(JobTasks)
        .distinct()
        .pluck('creator_id')
        .whereIn('id',task_ids)
        .populate()

        const users = await knex
        .fromORM(Users,'users')
        .select(
            ...knexPrefix(UserskVO.baseInfo())
        )
        .whereIn('id',creator_ids)
        .populate()
         
        return JsonApiResponse.new(users)
    }

    @Get('list/super')
    async listSuper(){
        const knex = knext(this)
        
        const users =  await knex
        .fromORM(Users,'users')
        .select(
            ...knexPrefix(UserskVO.baseInfo())
        )
        .whereJsonSupersetOf('roles',["super-admin"])
        //.where(raw)
        //.whereIn('id',creator_ids)
        .populate()
         
        return JsonApiResponse.new(users)
    }

    @Get('list/record')
    async record()
    {
        const knex = knext(this) //this.em.getKnex()

        const ids = await knex
        .fromORM(LoginRecoders)
        .pluck('user_id')
        .orderBy('times','desc')
        .limit(5)
        .populate()

        const users = await knex
        .fromORM(Users,'users')
        .select(
            ...knexPrefix(UserskVO.baseInfo())
        )
        .whereIn('id',ids)
        .populate()
         
        return JsonApiResponse.new(users)
    }

    @Get('list/phone')
    async phoneNumber(@Query() query)
    {
        const phone = query['phone_number']
        if(!phone) 
            return JsonApiResponse.new([])

        const knex = knext(this)
        let users : any =  knex
        .fromORM(Users,'users')
        .select(
            ...knexPrefix(UserskVO.baseInfo())
        )
        .whereILike('phone_number',`${phone}%`)
        
        console.log(users.toQuery())

        users = await users.populate()
         
        return JsonApiResponse.new(users)
    }
}
